﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Agr.CQRS.Infrastructure.ServiceBus;

namespace Tests.Agr.CQRS.SB
{
    [TestClass]
    public class When_Transaction_Is_Committed
    {
        ITransaction tx;

        [TestInitialize]
        public void TestInit()
        {
            //Given
            var sb = new ServiceBus();
            sb.Subscribe<someSortOfEvent>(handleEvent);
            
            tx = sb.OpenTransaction();
            sb.PublishTransactional(tx, new someSortOfEvent());
            sb.PublishTransactional(tx, new someSortOfEvent());
            
            //When
            tx.Commit();
        }

        [TestMethod()]
        public void Then_All_Events_In_Transaction_Are_Published()
        {
            Assert.AreEqual(2, handled);
        }

        
        [TestMethod()]
        public void Then_Committed_Events_Are_Disposed_Of()
        {
            tx.Commit(); //again
            Assert.AreEqual(2, handled); // no difference
        }


        private int handled = 0;
        private void handleEvent(someSortOfEvent ev) { handled++; }

        private class someSortOfEvent { }
    }
}
